package com.campus.dao.impl;

import com.campus.dao.ActivityDao;
import com.campus.model.Activity;
import com.campus.model.Teacher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
@Slf4j
public class ActivityDaoImpl implements ActivityDao {
    @Autowired
    private Connection conn;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public Integer addActivity(Activity activity) {
        String sql=" INSERT INTO t_manager_activity (name,category,title,creator,abstract,detailed_desc,status,created_time,pic_urls) " +
                " VALUES(?,?,?,?,?,?,?,?,?)";
        Object[] args = new Object[]{
                activity.getName(),
                activity.getCategory(),
                activity.getTitle(),
                activity.getCreator(),
                activity.getActivity_abstract(),
                activity.getDetailed_desc(),
                activity.getStatus(),
                activity.getCreated_time(),
                activity.getPic_urls(),
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer editActivity(Activity newactivity) {
        String sql = " UPDATE t_manager_activity SET name=?,category=?,title=?,creator=?,abstract=?,detailed_desc=?,pic_urls=? WHERE id=? ";
        Object[] args = new Object[]{
                newactivity.getName(),
                newactivity.getCategory(),
                newactivity.getTitle(),
                newactivity.getCreator(),
                newactivity.getActivity_abstract(),
                newactivity.getDetailed_desc(),
                newactivity.getPic_urls(),
                newactivity.getActivity_id()
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Integer publishActivity(int activityId) {
        String sql = " UPDATE t_manager_activity SET status=1 WHERE id=?";
        return jdbcTemplate.update(sql,activityId);
    }

    @Override
    public List<Activity> queryActivityByStatus(int status) {
        String sql = "SELECT * FROM t_manager_activity WHERE status = ? ORDER BY created_time DESC";
        return jdbcTemplate.query(sql,new Object[]{status}, new RowMapper<Activity>() {
            public Activity mapRow(ResultSet rs, int rowNum) throws SQLException {
                Activity activity = new Activity();
                activity.setActivity_id(rs.getInt("id"));
                activity.setName(rs.getString("name"));
                activity.setCategory(rs.getString("category"));
                activity.setTitle(rs.getString("title"));
                activity.setCreator(rs.getString("creator"));
                activity.setActivity_abstract(rs.getString("abstract"));
                activity.setDetailed_desc(rs.getString("detailed_desc"));
                activity.setCreator(rs.getString("creator"));
                activity.setStatus(rs.getInt("status"));
                activity.setCreated_time(rs.getTime("created_time"));
                activity.setPic_urls(rs.getString("pic_urls"));
                return activity;
            }
        });
    }

    @Override
    public List<Activity> queryActivityByName(String name) {
        String sql = "SELECT * FROM t_manager_activity WHERE name LIKE ? ORDER BY created_time DESC";
        return jdbcTemplate.query(sql,new Object[]{"%" + name + "%"}, new RowMapper<Activity>() {
            public Activity mapRow(ResultSet rs, int rowNum) throws SQLException {
                Activity activity = new Activity();
                activity.setActivity_id(rs.getInt("id"));
                activity.setName(rs.getString("name"));
                activity.setCategory(rs.getString("category"));
                activity.setTitle(rs.getString("title"));
                activity.setCreator(rs.getString("creator"));
                activity.setActivity_abstract(rs.getString("abstract"));
                activity.setDetailed_desc(rs.getString("detailed_desc"));
                activity.setCreator(rs.getString("creator"));
                activity.setStatus(rs.getInt("status"));
                activity.setCreated_time(rs.getTime("created_time"));
                activity.setPic_urls(rs.getString("pic_urls"));
                return activity;
            }
        });
    }
}
